package com.houtai.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.houtai.commonutils.Result;
import com.houtai.servicebase.exceptionhandler.DException;
import com.houtai.vod.Utils.ConstantVodUtils;
import com.houtai.vod.Utils.InitObject;
import com.houtai.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadVideo")
    public Result uploadVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideo(file);
        return Result.ok().data("videoId",videoId);
    }
    //根据视频ID进行删除
    @DeleteMapping("removeVideo/{id}")
    public Result removeVideo(@PathVariable String id){
        try{
            //初始化对象
            DefaultAcsClient client = InitObject.initVodClient("LTAI5tAvaXM4X9JrA4x37Uyv", "eqnHuHyfqfqwS4ZhGoWXM0guKv2dGU");
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return Result.ok();

        }catch (Exception e){
             throw new DException(20001,"删除视频失败");
        }

    }
    //删除多个视频
    @DeleteMapping("delete-batch")
    public Result deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeall(videoIdList);

        return Result.ok();
    }

    /**
     * xst
     * 根据视频id获取视频凭证
     */
    @GetMapping("getPlayAuth/{id}")
    public Result getPlayAuth(@Valid @PathVariable String id){
        try{
            //创建初始化对象
            DefaultAcsClient client =
                    InitObject.initVodClient("LTAI5tAvaXM4X9JrA4x37Uyv", "eqnHuHyfqfqwS4ZhGoWXM0guKv2dGU");
            //创建获取凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            return Result.ok().data("playAuth",playAuth);
        }catch(Exception e){
            throw new DException(20001,"获取凭证失败");
        }
    }
}
